<template>
  <el-dialog
    title="编辑角色"
    :visible.sync="dialogFormVisible"
    @closed="closeDialog"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="name" label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item
        prop="description"
        label="角色描述"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.description" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, editRoleByid } from '../../../api/setting'
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '角色名不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '角色名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   表单提交事件
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await editRoleByid(this.form)
            this.$message.success('修改成功')
          } else {
            await addRole(this.form)
            this.$message.success('新增成功')
          }
          this.dialogFormVisible = false
          this.$parent.getData()
          this.form = {
            name: '',
            description: ''
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    closeDialog() {
      this.form = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style></style>
